﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Xml;
using InnovTech.Util;
using System.Windows.Markup;

namespace InnovTech.Wpf.UserControl
{
    /// <summary>
    /// Interaction logic for XamlUIElementTree.xaml
    /// </summary>
    public partial class XamlUIElementTree : System.Windows.Controls.UserControl
    {
        public XamlUIElementTree()
        {
            InitializeComponent();
        }

        private string xamlFilePath;
        public string XamlFilePath
        {
            get { return xamlFilePath; }
            set
            {
                if (value == null) return;
                xamlFilePath = value;
                try
                {
                    XmlDocument doc = XmlUtil.GetDocument(xamlFilePath);
                    XmlElement node = doc.DocumentElement;
                    XmlElement e = doc.CreateElement("newRoot");
                    
                    e.AppendChild(node);
                    xamlFileTree.ItemsSource = e;
                    xamlFileTree.UpdateLayout();
                }
                catch (Exception ex)
                {
                    ShowMessage(ex.Message);
                }
                //int count = VisualTreeHelper.GetChildrenCount(
            }
        }

        private void ShowMessage(string message)
        {
            System.Windows.MessageBox.Show(message);
        }

        private void labelXmlNode_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            Label label = sender as Label;
            XmlElement xe = label.Tag as XmlElement;

            List<XmlAttributeSelector> selectors = new List<XmlAttributeSelector>();
            foreach (XmlAttribute attr in xe.Attributes)
            {
                selectors.Add(new XmlAttributeSelector(attr, xmlAttrs.ContainsKey(attr.LocalName), xmlAttrs));
            }

            dgAttributes.ItemsSource = selectors;
        }


        private Dictionary<string, XmlAttribute> xmlAttrs = new Dictionary<string, XmlAttribute>();

        
    }


    internal class XmlAttributeSelector
    {
        public XmlAttributeSelector(XmlAttribute attr, bool isSelected, Dictionary<string, XmlAttribute> attrs)
        {
            Attr = attr;
            Attrs = attrs;
            IsSelected = isSelected;
            
        }

        public XmlAttribute Attr { get; set; }
        private Dictionary<string, XmlAttribute> Attrs { get; set; }

        public string Name 
        { 
            get 
            { 
                return Attr.OwnerElement.LocalName + "." + Attr.Name; 
            } 
        }
        public string Value { get { return Attr.Value; } }

        private bool isSelected = false;
        public bool IsSelected { 
            get { return isSelected; }
            set 
            { 
                isSelected = value;
                if (isSelected)
                {
                    if (!Attrs.ContainsKey(Name))
                        Attrs.Add(Name, Attr);
                }
                else
                {
                    if (Attrs.ContainsKey(Name))
                        Attrs.Remove(Name);
                }
            }
        }
    }

    


}
